package com.base.cn.platform.os.controller.op.article;

import com.base.cn.platform.os.common.constants.CommonConstants;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.service.manage.op.article.OpArticleService;
import com.base.cn.platform.os.service.manage.op.article.OpArticleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资讯接口
 *
 * @author s.li
 * @date 2018/11/02
 */
@RestController
@RequestMapping("/api/article")
public class ApiArticleController extends ApiBaseController {

    @Autowired
    private OpArticleService opArticleService;
    @Autowired
    private OpArticleTypeService opArticleTypeService;
    @Autowired
    private CommonConstants commonConstants;

    /**
     * 获取资讯类型列表
     * @return Map<String,Object>
     */
    @RequestMapping("/findArticleTypeList")
    public Map<String,Object> findArticleTypeList(){
        Map<String,Object> params = new HashMap<>();
        params.put("status",1);
        List<Map<String,Object>> articleTypeList = opArticleTypeService.queryAllArticleType(params);
        return ResultUtil.SUCCESS(articleTypeList);
    }

    /**
     * 查询资讯列表
     * @param currentPage 页码
     * @param typeId 类型ID
     * @return Map<String,Object>
     */
    @RequestMapping("/findArticleList")
    public Map<String,Object> findArticleList(@RequestParam("currentPage") int currentPage,
                                              @RequestParam(value = "typeId",required = false,defaultValue = "0")BigDecimal typeId){
        Map<String,Object> params = new HashMap<>();
        params.put("status",0);
        params.put("releaseStatus",2);
        params.put("typeId",typeId);
        params.put("order",2);
        Map<String,Object> dataMap = opArticleService.queryArticleList(currentPage,params);
        return ResultUtil.SUCCESS(dataMap);
    }

    /**
     * 查询资讯接口
     * @param articleId 资讯ID
     * @return Map<String,Object>
     */
    @RequestMapping("/findArticleInfo")
    public Map<String,Object> findArticleInfo(@RequestParam("articleId") BigDecimal articleId){
        Map<String,Object> article = opArticleService.findArticleById(articleId,true,5);
        Map<String,Object> resultMap = ResultUtil.SUCCESS(article);
        resultMap.put("shareUrl",commonConstants.get(CommonConstants.MOBILE_PATH)+"/mobile/article/articleInfoById?articleId="+articleId);
        return resultMap;
    }
}
